package cn.ee.dfs.esb.service.impl;

import cn.ee.dfs.common.entity.DfsConstant;
import cn.ee.dfs.common.entity.QueryRequest;
import cn.ee.dfs.common.utils.RedisTool;
import cn.ee.dfs.common.utils.SortUtil;
import cn.ee.dfs.esb.entity.ServiceInfo;
import cn.ee.dfs.esb.entity.UserService;
import cn.ee.dfs.esb.mapper.ServiceInfoMapper;
import cn.ee.dfs.esb.service.IServiceInfoService;
import cn.ee.dfs.esb.service.IUserServiceService;
import cn.ee.dfs.monitor.service.IApiLogService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * Service实现
 *
 * @author TWJ
 * @date 2019-07-31 17:02:37
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class ServiceInfoServiceImpl extends ServiceImpl<ServiceInfoMapper, ServiceInfo> implements IServiceInfoService {

//    @Autowired
//    private ServiceInfoMapper serviceInfoMapper;

    @Autowired
    RedisTool redisTool;

    @Autowired
    IApiLogService apiLogService;

    @Autowired
    IUserServiceService userServiceService;


    @Override
    public IPage<ServiceInfo> findServiceInfos(QueryRequest request, ServiceInfo serviceInfo) {
//        LambdaQueryWrapper<ServiceInfo> queryWrapper = new LambdaQueryWrapper<>();
        // TODO 设置查询条件
        Page<ServiceInfo> page = new Page<>(request.getPageNum(), request.getPageSize());
        SortUtil.handlePageSort(request, page, "serviceId", DfsConstant.ORDER_ASC, false);
//        return this.page(page, queryWrapper);
        return this.baseMapper.findServiceDetailPage(page, serviceInfo);
    }

    /**
     * 用户申请的服务   查询
     */
    @Override
    public IPage<ServiceInfo> findUserServices(QueryRequest request, UserService userService) {
        Page<ServiceInfo> page = new Page<>(request.getPageNum(), request.getPageSize());
        return this.baseMapper.findUserServicesDetailPage(page, userService);
    }

    @Override
    public List<ServiceInfo> findServiceInfos(ServiceInfo serviceInfo) {
        LambdaQueryWrapper<ServiceInfo> queryWrapper = new LambdaQueryWrapper<>();
        // TODO 设置查询条件
        return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    @Transactional
    public void createServiceInfo(ServiceInfo serviceInfo) {
        serviceInfo.setCreateTime(new Date());
        this.save(serviceInfo);
    }

    @Override
    @Transactional
    public void updateServiceInfo(ServiceInfo serviceInfo) {
        serviceInfo.setUpdateTime(new Date());
        updateById(serviceInfo);
        HashMap<String, Object> map = new HashMap<>();
        map.put("name", serviceInfo.getName());
        map.put("spName", serviceInfo.getSpName());
        map.put("version", serviceInfo.getVersion());
        map.put("status", serviceInfo.getStatus());
        map.put("esbUrl", serviceInfo.getBindEsbPath());
        map.put("bznsUrl", serviceInfo.getBusinessPath());
        redisTool.hmset("svc:" + serviceInfo.getServiceId(), map);
//        this.saveOrUpdate(serviceInfo);
    }

    @Override
    @Transactional
    public void deleteServiceInfo(String[] serviceIds) {
        List<String> list = Arrays.asList(serviceIds);
        List<String> svcIds = new ArrayList<>();
        list.forEach(id -> {
            List<UserService> userServices = userServiceService.findUserServices(new UserService(Long.parseLong(id)));
            userServices.forEach(u -> {
                svcIds.add(u.getUserServiceId() + "");
            });
        });
        userServiceService.deleteUserService(svcIds.toArray(new String[]{}));
        this.removeByIds(list);
    }

    @Override
    public Integer getCallCount(Integer serviceId, String range) {
        return apiLogService.getCallCount(serviceId, range);
    }
}
